package com.example.camerapush.util;

import android.content.Context;
import android.content.res.AssetFileDescriptor;
import android.media.AudioManager;
import android.media.MediaPlayer;

import com.example.camerapush.R;

import java.io.IOException;

/**
 * Author:lsh
 * Version: 1.0
 * Description:
 * Date: 2018/2/22
 */

public class VolumeUtils {

    private final MediaPlayer mMediaPlayer;

    public VolumeUtils(Context context) {
        //这个我定义了一个成员函数
        mMediaPlayer = new MediaPlayer();

        mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
        mMediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mp) {
                mp.seekTo(0);
            }
        });


        AssetFileDescriptor file = context.getResources().openRawResourceFd(
                R.raw.lee);

        try {
            mMediaPlayer.setDataSource(file.getFileDescriptor(), file.getStartOffset(), file.getLength());

            file.close();

//            mediaPlayer.setVolume(BEEP_VOLUME, BEEP_VOLUME);
            mMediaPlayer.prepare();

        } catch (IOException ioe) {
//            mediaPlayer = null;
        }
    }

    public void play() {
        mMediaPlayer.start();
        mMediaPlayer.setVolume(1.0f, 1.0f);
    }
}
